<?php

/*

Drop Box (drop_box.php)

*/

$rank_check = 1;
$page_title = "Drop Box";
include "header.inc.php";

print "$openHTML";

if (!$page)
{
	$page_title = "Drop Box - About to Play";
	$findPage = fetch("SELECT * FROM game_pages WHERE page_title = '$page_title' AND game = 	'$game'");
	if (!$findPage[id])
	{
		print "<P>Welcome to the Drop Box. Here you simply choose any of your items and put 	it into the box, after you drop it in the box, it wil give the previous item that was put in 	:)</P>";
	}
	else
	{
		include "showpage.inc.php";
		print "$findPage[page_info]";
	}

	print "<p align=center><form action=drop_box.php?game=$game&page=drop method=post><select 	name=drop_item><option>Choose an item";

	$findItems = mysql_query("SELECT * FROM usersitems2 WHERE owner = '$userid' AND game = '$game'");
	while ($getItems = mysql_fetch_array($findItems))
	{
		$get_item = fetch("SELECT * FROM items2 WHERE id = '$getItems[item_id]' AND game = '$game'");
		$itemList .= "<option value=$getItems[id]>$get_item[item_name]";
	}
	print "$itemList</select><input type=submit value=\"Submit\"></form></p>";
}

if ($page == "drop")
{
	$page_title = "Drop Box - Just Played";
	$findBox = fetch("SELECT * FROM drop_box WHERE game = '$game'");
	$findYourItem = fetch("SELECT * FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND id = '$drop_item'");
	if (!$findYourItem[id])
	{
		die("Please put in  a real item. $closeHTML");
	}
	$get_item = fetch("SELECT * FROM items2 WHERE id = '$findBox[item_id]' AND game = '$game'");
	$get_item2 = fetch("SELECT * FROM items2 WHERE id = '$findYourItem[item_id]' AND game = '$game'");
	if ($findBox[item_donater] == "$userid")
	{
		die("You just put the last item in the box, please wait.$closeHTML");
	}
	$findPage = fetch("SELECT * FROM game_pages WHERE page_title = '$page_title' AND game = 	'$game'");
	if (!$findPage[id])
	{
		print "<P>You just put your item into the drop box and receieved this item back:</P>";
	}
	else
	{
		include "showpage.inc.php";
		print "$findPage[page_info]";
	}

	if (!$findBox[id])
	{
		mysql_query("INSERT INTO drop_box (game) VALUES ('$game')");
		print "<p align=center><b><i>Nothing :(</i></b></p>";
	}

	mysql_query("UPDATE drop_box SET item_id = '$get_item2[id]' WHERE game = '$game'");
	mysql_query("UPDATE drop_box SET item_donater = '$userid' WHERE game = '$game'");

	mysql_query("DELETE FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND id = '$drop_item'");

	if ($findBox[id])
	{
	mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$get_item[id]','$get_item[parts]','$game')");

	print "<p align=center><img src=$base_url/images/user_images/opg_$game/items/item_$get_item[id].gif><Br>$get_item[item_name]</p>";
	}

	print "<p align=center><a href=games.php?game=$game>Back to the games!</a></p> $closeHTML";
}
?>